﻿// Server Message

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Collections;
using System.Threading;

namespace sfsocket.corelib.message
{
    /// <summary>
    /// SMessage is a LTV message, the length include legth of T and V.
    /// i.e. 
    /// 0x00000006  - length = len(type) + len(value)
    /// 0x00000001  - type (4byte)
    /// 0xffff      - value (2byte
    /// </summary>
    public class BaseMessage
    {
        public int Length;
        public int MessageType;

        public BaseMessage()
        { }

        // public abstract BaseMessage Parse(byte[] _headerBuf);


        public virtual byte[] GetBytes()
        {
            return null;
        }

        //public virtual BaseMessage Parse(byte[] _buff)
        //{
        //    if (_buff == null)
        //        return null;

        //    BaseMessage msg = null;

        //    // get length and type
        //    //Stream memStream = new MemoryStream(_buff);
        //    //BinaryReader reader = new BinaryReader(memStream);
        //    //msg.Length = IPAddress.NetworkToHostOrder(reader.ReadInt32());
        //    //// validate the packet 
        //    //if (msg.Length != _buff.Length - 4)
        //    //{
        //    //    msg = null;
        //    //    throw new ArgumentException("invalid packet! length is not valid!");
        //    //}
        //    //msg.MessageType = IPAddress.NetworkToHostOrder(reader.ReadInt32());

        //    //switch (msg.MessageType)
        //    //{
        //    //}
        //    return msg;

        //}

        public virtual void Dump() 
        { }

        public virtual void DumpHex()
        {
            Console.WriteLine("Hex Dump:");
            foreach (byte b in GetBytes())
            {
                Console.Write("0x{0:X}, ", b);
            }
        }
    }


    public class SMessageUnitTest
    {
        //public static void TestConstrcutFromFields()
        //{
        //    byte[] val_buf = new byte[4] { 0xff, 0xff, 0xff, 0xff};
        //    BaseMessage sm = new BaseMessage(0x00, val_buf);
        //    sm.Dump();
        //    sm.DumpHex();
        //}



        //public static void TestConstructFromBytes()
        //{
        //    byte[] val_buf = new byte[] {
        //        0x00, 0x00, 0x00, 0x06,     // length
        //        0x00, 0x00, 0x00, 0x00,     // msg type 
        //        0xff, 0xff};                // value
        //    BaseMessage sm = BaseMessage.Parse(val_buf);
        //    sm.Dump();
        //    sm.DumpHex();
        
        //}

        //// following construction should fail due to not valid
        //// will raise an exception
        //public static void TestConstructFromBytesFail()
        //{
        //    byte[] val_buf = new byte[] { 
        //        0x00, 0x00, 0x00, 0x06,
        //        0x00, 0x00, 0x00, 0x02,
        //        0xff, 0xff, 0xff, 0xff};
        //    BaseMessage sm = BaseMessage.Parse(val_buf);
        //    sm.Dump();
        //}
    }

}